#!/bin/sh

e2e_use_extensions() {
  true
}

e2e_test_install() {
  CLUSTER_NAME="extensions"
  POSTGIS_VERSION="$(get_latest_version_of_extension postgis 14.9 || true)"
  POSTGIS_RASTER_VERSION="$(get_latest_version_of_extension postgis_raster 14.9 || true)"
}

e2e_test() {
  if [ -z "$POSTGIS_VERSION" ] || [ -z "$POSTGIS_RASTER_VERSION" ]
  then
    echo "Skipping extensions-extra-mounts-and-overwrite since postgis not available for $(uname -m)"
    return
  fi

  run_test "Check extension with extra mount is installed and available when booting" check_extension_with_extra_mount_installed_when_booting

  run_test "Check extension with same shared library is installed and available when added" check_extension_with_same_shared_library_when_added
}

check_extension_with_extra_mount_installed_when_booting() {
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" "2" \
    --set-string 'cluster.postgres.version=14.9' \
    --set-string 'cluster.postgres.extensions[0].name=postgis' \
    --set-string "cluster.postgres.extensions[0].version=$POSTGIS_VERSION" \
    --set-string configurations.postgresconfig.postgresql\\.conf.shared_preload_libraries=

  kubectl delete pod -n "$CLUSTER_NAMESPACE" --all
  wait_pods_running "$CLUSTER_NAMESPACE" "2"
  wait_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"

  try_function wait_until eval '[ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq "select(.status != null and .status.podStatuses != null) | .status.podStatuses[]
      | select(.installedPostgresExtensions != null) | .installedPostgresExtensions[]
      | select(.name == \"postgis\" and .version == \"$POSTGIS_VERSION\") | length" \
    | jq -s "length")" = 2 ]'
  if "$RESULT"
  then
    success "postgis ($POSTGIS_VERSION) was installed"
  else
    fail "postgis ($POSTGIS_VERSION) was not installed"
  fi

  try_function run_query -i 0 -q 'CREATE EXTENSION postgis;'
  if "$RESULT"
  then
    success "postgis ($POSTGIS_VERSION) was created"
  else
    fail "postgis ($POSTGIS_VERSION) was not created"
  fi

  if wait_until -t "$((E2E_TIMEOUT / 10))" eval '! kubectl wait sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --for condition=PendingRestart --timeout 0'
  then
    success "Cluster is not pending restart after booting with postgis ($POSTGIS_VERSION)"
  else
    fail "Cluster is pending restart after booting with postgis ($POSTGIS_VERSION)"
  fi

  try_function run_query -i 0 -q "SELECT 'mpoint_7', ST_AsEWKT(ST_GeomFromGML('<gml:MultiPoint srsName=\"EPSG:27582\"><gml:pointMember><gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point srsName=\"EPSG:27562\"><gml:coordinates>400000,5000000</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint>'));"
  if "$RESULT"
  then
    success "postgis ($POSTGIS_VERSION) has extra mount installed correctly"
  else
    fail "postgis ($POSTGIS_VERSION) has extra mount not installed correctly"
  fi
}

check_extension_with_same_shared_library_when_added() {
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" "2" \
    --reset-values \
    --set-string 'cluster.postgres.version=14.9' \
    --set-string 'cluster.postgres.extensions[0].name=postgis' \
    --set-string "cluster.postgres.extensions[0].version=$POSTGIS_VERSION" \
    --set-string 'cluster.postgres.extensions[1].name=postgis_raster' \
    --set-string "cluster.postgres.extensions[1].version=$POSTGIS_RASTER_VERSION" \
    --set-string configurations.postgresconfig.postgresql\\.conf.shared_preload_libraries=

  try_function wait_until eval '[ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq "select(.status != null and .status.podStatuses != null) | .status.podStatuses[]
      | select(.installedPostgresExtensions != null) | .installedPostgresExtensions[]
      | select(.name == \"postgis_raster\" and .version == \"$POSTGIS_RASTER_VERSION\") | length" \
    | jq -s "length")" = 2 ]'
  if "$RESULT"
  then
    success "postgis_raster ($POSTGIS_RASTER_VERSION) was installed"
  else
    fail "postgis_raster ($POSTGIS_RASTER_VERSION) was not installed"
  fi

  try_function run_query -i 0 -q 'CREATE EXTENSION postgis_raster;'
  if "$RESULT"
  then
    success "postgis_raster ($POSTGIS_RASTER_VERSION) was created"
  else
    fail "postgis_raster ($POSTGIS_RASTER_VERSION) was not created"
  fi

  if wait_until -t "$((E2E_TIMEOUT / 10))" eval '! kubectl wait sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --for condition=PendingRestart --timeout 0'
  then
    success "Cluster is not pending restart after installing postgis_raster ($POSTGIS_RASTER_VERSION)"
  else
    fail "Cluster is pending restart after installing postgis_raster ($POSTGIS_RASTER_VERSION)"
  fi
}
